package com.code.aspectj.aspectj;

import android.app.Activity;
import android.text.TextUtils;

import com.code.aspectj.AppInterceptor;
import com.code.aspectj.annotation.Check;
import com.code.aspectj.util.CheckType;
import com.code.aspectj.util.P;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import java.lang.reflect.Method;

/**
 * Created by RLZ
 * on 2021/12/24
 */
@Aspect
public class CheckAspect {

    private static final String APP_NAME = "cn.coolyou.liveplus.LiveApp";
    private static final String ACT_NAME = "com.seca.live.activity.BaseFragmentActivity";
    private static final String FR_NAME = "com.seca.live.fragment.BaseFragment";

    @Pointcut("within(@com.code.aspectj.annotation.Check *)")
    public void withinAnnotatedClass() {
    }

    @Pointcut("execution(!synthetic * *(..)) && withinAnnotatedClass()")
    public void methodInsideAnnotatedType() {
    }


    @Pointcut("execution(@com.code.aspectj.annotation.Check * *(..)) || methodInsideAnnotatedType()")
    public void method() {
    }


    @Around("method() && @annotation(check)")
    public Object aroundJoinPoint(ProceedingJoinPoint joinPoint, Check check) {
        CheckType value = check.value();
        P.d("aspect_check", "获取到type : " + value.name());
        switch (value) {
            case LOGIN:
                return disposeLogin(joinPoint);
            case OTHER:
                return disposeOther(joinPoint, check.otherType());
        }
        return null;
    }

    private Object disposeOther(ProceedingJoinPoint joinPoint, int type) {
        try {
            if (type == 0 || AppInterceptor.getCheckHandler() == null) return joinPoint.proceed();

            boolean result = AppInterceptor.getCheckHandler().handle(type);

            return result ? null : joinPoint.proceed();

        } catch (Throwable t) {
            t.printStackTrace();
        }
        return null;
    }

    private Object disposeLogin(ProceedingJoinPoint joinPoint) {
        try {
            //获取token信息
            Class<?> appClass = Class.forName(APP_NAME);

            //使用单例对象
            Method instance = appClass.getMethod("getInstance");
            Object app = instance.invoke(null);

            Method method = appClass.getMethod("getToken");
            method.setAccessible(true);
            Object token = method.invoke(app);
            P.d("aspect_check", "获取到method : " + method.getName());

            if (token != null) {
                //已经登录
                return joinPoint.proceed();
            } else {
                Object pointThis = joinPoint.getThis();
                Class<?> currentClass = Class.forName(pointThis instanceof Activity ? ACT_NAME : FR_NAME);
                Method showMethod = currentClass.getMethod("openLoginDialog");
                showMethod.setAccessible(true);
                showMethod.invoke(pointThis);
            }

        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }
}
